#include <cvirte.h>		
#include <userint.h>
#include "xielv.h"

static int panelHandle;

int main(int argc, char *argv[]) {
	if (InitCVIRTE(0, argv, 0) == 0)
		return -1; /* out of memory */
	if ((panelHandle = LoadPanel(0, "xielv.uir", PANEL)) < 0)
		return -1;
	DisplayPanel(panelHandle);
	RunUserInterface();
	DiscardPanel(panelHandle);
	return 0;
}

int CVICALLBACK Quitcall (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){
	switch (event){
		case EVENT_COMMIT:
		QuitUserInterface (0);
		break;
	}
	return 0;
}

int CVICALLBACK Startcall (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){	
	double X0,X1,Y0,Y1,k;
	switch (event){
		case EVENT_COMMIT:
		SetCtrlVal (panelHandle, PANEL_LED, 0);
		GetCtrlVal (panelHandle, PANEL_X0,&X0);
		GetCtrlVal (panelHandle, PANEL_X1,&X1);
		GetCtrlVal (panelHandle, PANEL_Y0,&Y0);
		GetCtrlVal (panelHandle, PANEL_Y1,&Y1);
		if(X0!=X1) {
			k=(Y1-Y0)/(X1-X0);
			SetCtrlVal (panelHandle, PANEL_xielv, k);}
		else {
			k=(Y1-Y0)/(X1-X0);
			SetCtrlVal (panelHandle, PANEL_LED, 1);
			SetCtrlVal (panelHandle, PANEL_xielv, k);}
		break;
	}
	return 0;
}
